package com.njrsrc.android.tasks;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

public class ShowUserInfoTask extends AsyncTask<Object, Integer, Void> {
	private String url;
	private Handler mHandler;
	private UserInfo userInfo;

	@Override
	protected Void doInBackground(Object... params) {
		if (params.length < 3) {
			return null;
		}
		mHandler = (Handler) params[0];
		url = (String) params[1];
		userInfo = (UserInfo) params[2];
		JSONObject jsonObject = Helpers.getJSONArrayByGet(url);
		Message msg = mHandler.obtainMessage();
		if (jsonObject != null) {
			try {
				if (jsonObject == null || jsonObject.toString().contains("error_code")) {
					// 返回错误
					userInfo.setError_code(jsonObject.getString("error_code"));
					userInfo.setError_message(jsonObject.getString("error_message"));
					msg.what = MsgID.SHOW_USER_INFO_FAIL;
				} else {
					userInfo.setUser_id(jsonObject.getString("user_id"));
					userInfo.setLogin_name(jsonObject.getString("login_name"));
					userInfo.setName(jsonObject.getString("name"));
					userInfo.setEmail(jsonObject.getString("email"));
					userInfo.setPic_url(jsonObject.getString("pic_url"));
					userInfo.setHr_letter_count(jsonObject.getString("unread_letters"));
					// userInfo.setPic_url("http://jpp2.imghb.com/pic/pic/8/45/44/1400465708454447_a602x602.jpg");
					userInfo.setDang_an_id(jsonObject.getString("dang_an_id"));
					userInfo.setBe_searched(jsonObject.getString("be_searched"));
					userInfo.setBe_looked(jsonObject.getString("be_looked"));
					userInfo.setResume_modified_at(jsonObject.getString("resume_modified_at"));
					msg.what = MsgID.SHOW_USER_INFO_SUCCESS;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			mHandler.sendMessage(msg);
		}
		return null;
	}
}